package com.ciis.system.module.service.impl;

import com.ciis.base.Constants;
import com.ciis.base.page.Page;
import com.ciis.base.system.ControllerContext;
import com.ciis.system.module.domain.ModuleMenuNode;
import com.ciis.system.module.domain.ModuleMenuTree;
import com.ciis.system.module.domain.TSysModule;
import com.ciis.system.module.mapper.ITSysModuleMapper;
import com.ciis.system.module.service.ISysModuleService;
import com.ciis.system.user.domain.TSysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zhangdajun
 * Date: 14-6-19
 * Time: 下午8:39
 * To change this template use File | Settings | File Templates.
 */
@Service("sysModuleService")
public class SysModuleServiceImpl implements ISysModuleService {

    @Autowired
    ITSysModuleMapper sysModuleMapper;

    @Override
    public void save(TSysModule module) throws Exception {
        sysModuleMapper.save(module);
    }

    @Override
    public void update(TSysModule module) throws Exception {
        sysModuleMapper.update(module);
    }

    @Override
    public void deleteById(String ids) throws Exception {
        String idArray[] = ids.split(",");
        for(String id : idArray){
            sysModuleMapper.deleteById(id);
        }
    }

    @Override
    public Page<TSysModule> findPage(Page<TSysModule> pager, TSysModule module) throws Exception {
        sysModuleMapper.findPageList(pager,module);
        System.out.println(pager.getRows().size());
        return (Page<TSysModule>)pager;
    }

    @Override
    public List<TSysModule> findModuleListByPid(String pId) throws Exception {
        TSysUser user = (TSysUser)ControllerContext.getContext().getSession().getAttribute(Constants.SESSION_USER);
        HashMap<String,String> param = new HashMap<String,String>();
        param.put("pId",pId);
        param.put("userId",user.getId());
        return sysModuleMapper.findModuleListByPid(param);
    }

    @Override
    public List<TSysModule> findModuleListById(String id) throws Exception {
        TSysUser user = (TSysUser)ControllerContext.getContext().getSession().getAttribute(Constants.SESSION_USER);
        HashMap<String,String> param = new HashMap<String,String>();
        param.put("id",id);
        param.put("userId",user.getId());
        return sysModuleMapper.findModuleListById(param);
    }

    @Override
    public List<TSysModule> findModuleListByLevel(String level) throws Exception {
        TSysUser user = (TSysUser) ControllerContext.getContext().getSession().getAttribute(Constants.SESSION_USER);
        HashMap<String,String> param = new HashMap<String,String>();
        param.put("level",level);
        param.put("userId",user.getId());
        return sysModuleMapper.findModuleListByLevel(param);
    }

    @Override
    public ModuleMenuTree findModuleTreeByPid(String pId) throws Exception {
        // 根据父Id获取模块信息，封装模块树返回
        ModuleMenuTree menuTree = buildModuleMenu(pId);
        menuTree.setMenuList(findModuleNodeByPid(pId));
        return menuTree;
    }

    /**
     * 查询父节点信息
     * @param pId
     * @return
     * @throws Exception
     */
    private List<ModuleMenuNode> findModuleNodeByPid(String pId) throws Exception{
        List<TSysModule> moduleList = this.findModuleListByPid(pId);
        if(moduleList == null || moduleList.size() == 0){
            return null;
        }
        List<ModuleMenuNode> moduleNodeList = new ArrayList<ModuleMenuNode>();
        for(TSysModule module : moduleList){
            ModuleMenuNode menuNode = buildMenuNode(module);
            moduleNodeList.add(menuNode);
            menuNode.setChildren(findModuleNodeByPid(module.getId()));
            menuNode.setSubMenuList(menuNode.getChildren());
            if(menuNode.getChildren() == null){
                menuNode.setFolder(false);
                menuNode.setNode(true);
            }
        }
        return moduleNodeList;
    }

    private ModuleMenuTree buildModuleMenu(String pId){
        ModuleMenuTree menuTree = new ModuleMenuTree();
        menuTree.setMenu(pId);
        menuTree.setMenuId(pId);
        menuTree.setNeedSub(true);
        return menuTree;
    }


    private ModuleMenuNode buildMenuNode(TSysModule module){
        ModuleMenuNode menuNode = new ModuleMenuNode();
        menuNode.setAuthId("1");
        menuNode.setClick("1");
        menuNode.setCreateDate("");
        menuNode.setCreateUser("admin");
        menuNode.setExpanded(true);
        menuNode.setExtension("1");
        menuNode.setFolder(true);
        menuNode.setKey(module.getId());
        menuNode.setMenuDes(module.getName());
        menuNode.setMenuIcon(module.getIcon());
        menuNode.setMenuId(module.getId());
        menuNode.setMenuName(module.getName());
        menuNode.setMenuOrder(module.getLevel());
        menuNode.setMenuPath(module.getPath());
        menuNode.setMenuStyle(module.getIcon());
        menuNode.setModuleId("1");
        menuNode.setParentMenuId(module.getP_id());
        menuNode.setTitle(module.getName());
        menuNode.setMenuStyle("1");
        return menuNode;
    }
}
